package com.example.hxpc.guidancemanagement.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.activity.NumberListActivity;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.base.NumberInfo;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.LogoutT;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

import java.util.List;

/**
 * Created by tao on 2018/3/13.
 * 成员列表适配器
 */

public class NumberListAdapter extends BaseAdapter implements View.OnClickListener,BaseApi{

    //item内容
    private List<NumberInfo> number;
    //获取上下文，用来获得convertView
    private Context context;
    //屏幕宽度，由于用的是HorizontalScrollView，所以按钮选项应该在屏幕外
    private int mScreenWidth;

    private OkHttpTools okHttpTools = new OkHttpTools();

    public NumberListAdapter(Context context , List<NumberInfo> number , int mScreenWidth){
        this.context = context;
        this.mScreenWidth = mScreenWidth;
        this.number = number;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        LogoutT.logout("position  "+ position);
        switch (v.getId()){
            case R.id.delete:
                okHttpTools.removeNumber(this,number.get(position).getNumberTelephone());
                number.remove(position);
                break;
        }
        //刷新ListView内容
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return number.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            //初始化holder
            viewHolder = new ViewHolder();
            //获取设置的view
            convertView = LayoutInflater.from(context).inflate(R.layout.number_list_item,parent,false);

            viewHolder.itemHorizontalScrollView = (HorizontalScrollView) convertView.findViewById(R.id.hsv);

            //item内容（没左侧滑）
            viewHolder.normalItemContentLayout = convertView.findViewById(R.id.ll_content);
            viewHolder.img_photo = (ImageView) convertView.findViewById(R.id.user_photo);
            viewHolder.te_name = (TextView) convertView.findViewById(R.id.number_name);
            viewHolder.te_type = (TextView) convertView.findViewById(R.id.number_type);

            //左侧滑操作执行
            viewHolder.actionLayout = convertView.findViewById(R.id.ll_action);
            viewHolder.bt_delete = (Button) convertView.findViewById(R.id.delete);
            viewHolder.bt_delete.setTag(position);

            LayoutParams lp = viewHolder.normalItemContentLayout.getLayoutParams();
            lp.width = mScreenWidth;

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(number.get(position).getNumberPhoto()).into(viewHolder.img_photo);
        viewHolder.te_name.setText(number.get(position).getNumberName());
        viewHolder.te_type.setText(number.get(position).getNumberType());
        //设置监听事件
        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_UP:
                        //获得ViewHolder
                        ViewHolder holder = (ViewHolder) v.getTag();
                        //获得HorizontalScrollView滑动的水平方向值
                        int scrollX = holder.itemHorizontalScrollView.getScrollX();
                        //获得操作区的长度
                        int actionW = holder.actionLayout.getWidth();
                        //注意使用smoothScrollTo,这样效果看起来比较圆滑，不生硬
                        //如果水平方向的移动值<操作区域的长度的一般，就复原
                        if (scrollX<actionW/2){
                            holder.itemHorizontalScrollView.smoothScrollTo(0,0);
                        }else {//否则的话显示操作区域
                            holder.itemHorizontalScrollView.smoothScrollTo(actionW,0);
                        }
                        return true;
                }
                return false;
            }
        });
        //这里防止删除一条item后，ListView处于操作状态，直接还原
        if (viewHolder.itemHorizontalScrollView.getScaleX()!=0){
            viewHolder.itemHorizontalScrollView.scrollTo(0,0);
        }
        viewHolder.bt_delete.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void backMsg(String success, JsonBase jsonBase) {
        if (success.equals("true")){
            ToastTools.toastMsg(context,jsonBase.getMsg());
        }else {
            ToastTools.toastMsg(context,jsonBase.getMsg());
        }
    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {

    }

    public class ViewHolder{

        public HorizontalScrollView itemHorizontalScrollView;

        /**
         * 正常（没左侧滑）情况的item内容布局
         */
        public View normalItemContentLayout;
        public ImageView img_photo;
        public TextView te_name;
        public TextView te_type;
        /**
         * 左侧滑出现的item内容布局
         */
        public View actionLayout;
        public Button bt_delete;
    }

}
